<?php
/**
 *    我的预报系统控制器
 *    @author liangcajian QQ632324010
 */
define('NUM_PER_PAGE', 20);        // 每页显示数量 

class My_kdqujianApp extends MemberbaseApp
{
  function __construct()
  {
	  $this->My_kdqujianApp();
  }
  
  function My_kdqujianApp()
  {
	  parent::__construct();
	  $this->_kd_qujian_mod =& m('kd_qujian');
  }	
    function index()
    {
        /* 搜索条件 */
        $conditions = "user_id = " . $this->visitor->get('user_id');
        if (isset($_GET['key']) && htmldecode($_GET['key'])){
            $conditions .= " AND (lianxiren LIKE '%".htmldecode($_GET['key'])."%' OR mobilphone LIKE '%".htmldecode($_GET['key'])."%')";
        }
        if (is_numeric($_GET['status']) && $_GET['status']> -1){
            $conditions .= " AND status = '".intval($_GET['status'])."' ";
        }		
		//录入时间
		if ($_GET['add_time_from']){
			$conditions.=" AND addtime>=".strtotime($_GET["add_time_from"]." 00:00:00")." ";
		}
		if ($_GET['add_time_to']){
			$conditions.=" AND addtime<=".strtotime($_GET["add_time_to"]." 23:59:59")." ";
		}	

		//更新排序
		if (isset($_GET['sort']) && isset($_GET['order'])){
			$sort  = strtolower(htmldecode($_GET['sort']));
			$order = strtolower(htmldecode($_GET['order']));
			if (!in_array($order,array('asc','desc'))){
			 $sort  = 'qujian_id';
			 $order = 'desc';
			}
		}else{
			$sort  = 'qujian_id';
			$order = 'desc';
		}	 
		$pagenum = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : NUM_PER_PAGE;
		$this->assign('pagenum', $pagenum);	  
		$page = $this->_get_page($pagenum); 
		$qujians = $this->_kd_qujian_mod->find(array(
			'conditions'    => $conditions,
			'limit'         => $page['limit'],  //获取当前页的数据
			'order'         => "$sort $order",
			'count'         => true //允许统计
		)); 

		//print_r("$sort $order");
		$page['item_count'] = $this->_kd_qujian_mod->getCount(); //获取统计的数据
		$this->_format_page($page);  
		$this->assign('page_info', $page); //将分页信息传递给视图，用于形成分页条
		$this->assign('qujians', $qujians);	 
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
									  'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));	   	  
       
	    /* 当前用户中心菜单 */
        $this->_curitem('my_kdqujian');
        /* 当前所处子菜单 */
        $this->_curmenu('kdqujian_list');
        $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('kdqujian_list'));
        $this->display('my_kdqujian.index.html');
    }

  /**
   *    添加地址
   *
   *    @author liangcajian QQ632324010
   *    @return    void
   */
  function add()
  {
	  if (!IS_POST)
	  {
		  //到货仓库
		  $category_mod =& m('zonghecategory');
		  $category = $category_mod->get_list(32);
		  $this->assign('category', $category);

		 /* 会员信息取出来 */
		  $member_mod =& m('member');
		  $member = $member_mod->get_info($this->visitor->get('user_id'));
		  $this->assign('member', $member);
		
		  /* 当前用户中心菜单 */
		  $this->_curitem('my_kdqujianadd');
		  /* 当前所处子菜单 */
		  $this->_curmenu('add_kdqujian');
		  $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
									  'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));	   	  
		  
		  $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('add_kdqujian'));
		  $this->display('my_kdqujian.form.html');			  
	  }
	  else
	  {
		//检查表单是否已正确填写了
		if ($this->check_form()==true)
		{
			$data = array(
			   'cangku' => htmldecode($_POST['cangku']),
			   'user_id' => $this->visitor->get('user_id'),
			   'user_name' => $this->visitor->get('user_name'),					   
			   'yuyueTime' => strtotime($_POST['yuyueTime']),
			   'qujianNumber' => htmldecode($_POST['qujianNumber']),
			   'lianxiren' => htmldecode($_POST['lianxiren']),
			   'mobilphone' => htmldecode($_POST['mobilphone']),
			   'Email' => htmldecode($_POST['Email']),
			   'countrys' => htmldecode($_POST['txt_country']).'//'.htmldecode($_POST['txt_province']).'//'.htmldecode($_POST['txt_city']),//国家/地区/省/市
			   'address' => htmldecode($_POST['address']),
			   'postoffice' => htmldecode($_POST['postoffice']),
			   'notes' => htmldecode($_POST['notes']),		
			   'addtime' => time(),	
			   'updatetime' => time(),		
			);
			$qujian_id = $this->_kd_qujian_mod->add($data);
			if ($this->_kd_qujian_mod->get_error()){
				$this->show_warning($this->_kd_qujian_mod->get_error());
				return;
			}						

			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '添加预约取件,ID:'.$qujian_id, 0);//添加操作日志	
			
			$this->show_message('add_kdqujian_successed',
				'back_list',    'index.php?app=my_kdqujian',
				'continue_add', 'index.php?app=my_kdqujian&amp;act=add'
             );			
		}
	  }
  }

  //检查表单的必填项
  function check_form()
  {  
	  if (!$_POST['cangku']){ $this->show_warning('请选择转运类别'); return false; }
	  if (!$_POST['yuyueTime']){ $this->show_warning('请选择预约时间'); return false; }
	  //判断预约取件的有效时间为30天内
	  $now_data = strtotime(date("Y-m-d", gmtime()));
	  if (strtotime($_POST['yuyueTime']) < $now_data){ $this->show_warning('预约时间不能小于'.date("Y-m-d", gmtime())); return false; }
	  if ( (strtotime($_POST['yuyueTime']) - $now_data) > 2592000){ $this->show_warning('预约时间不能超过当前时间30天'); return false; }
	  
	  if (!$_POST['qujianNumber']){ $this->show_warning('请选择取货件数'); return false; }
	  if (!$_POST['lianxiren']){ $this->show_warning('请输入联系人'); return false; }
	  if (!$_POST['mobilphone']){ $this->show_warning('请输入联系人电话'); return false; }
	  if (!$_POST['Email']){ $this->show_warning('请输入联系人邮箱'); return false; }
	  if (empty($_POST['txt_country'])){ $this->show_warning('请输入国家/地区'); return false; }
	  if (empty($_POST['txt_province'])){ $this->show_warning('请输入省/州'); return false; }
	  if (empty($_POST['txt_city'])){ $this->show_warning('请输入城市'); return false; }
	  if (empty($_POST['address'])){ $this->show_warning('请输入详细地址'); return false; }
  
	  return true;
  }  
  
  
  //删除数据
  function drop()
  {
	  $id = isset($_GET['id']) ? htmldecode($_GET['id']) : '';
	  if (!$id)
	  {
		  $this->show_warning('请选择需要删除的记录');
		  return;
	  }
	  $this->_kd_qujian_mod->drop("user_id = " .$this->visitor->get('user_id') . ' AND status = 0 AND ' . db_create_in($id,'qujian_id'));
	  if ($this->_kd_qujian_mod->has_error())
	  {
		  $this->show_warning($this->_kd_qujian_mod->get_error());
		  return;
	  }

	  $this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '删除预约取件,ID:'.$id, 0);//添加操作日志	
	  
	  $this->show_message('drop_ok' );	
  }  
  /**
   *    三级菜单
   *
   *    @author liangcajian QQ632324010
   *    @return    void
   */
  function _get_member_submenu()
  {
	  $menus = array(
		  array(
			  'name'  => 'kdqujian_list',
			  'url'   => 'index.php?app=my_kdqujian',
		  ),
		  array(
			  'name'  => 'add_kdqujian',
			  'url'   => 'index.php?app=my_kdqujian&act=add',
		  ),
	  );
	if (ACT == 'edit')
	  {
		  $menus[] = array(
			  'name' => 'edit_kdqujian',
			  'url'  => '',
		  );
	  }
	  return $menus;
  }
  
}
?>